"""
add order columns to folder tables

Revision ID: 20251118_01
Revises: 
Create Date: 2025-11-18 11:35:00
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = '20251118_01'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Add columns with a server default for backfilling existing rows
    with op.batch_alter_table('folders') as batch_op:
        batch_op.add_column(sa.Column('order', sa.Integer(), nullable=False, server_default='0'))
    with op.batch_alter_table('knowledge_folders') as batch_op:
        batch_op.add_column(sa.Column('order', sa.Integer(), nullable=False, server_default='0'))
    with op.batch_alter_table('review_folders') as batch_op:
        batch_op.add_column(sa.Column('order', sa.Integer(), nullable=False, server_default='0'))

    # Optional: drop the server_default so future inserts come from application logic
    with op.batch_alter_table('folders') as batch_op:
        batch_op.alter_column('order', server_default=None)
    with op.batch_alter_table('knowledge_folders') as batch_op:
        batch_op.alter_column('order', server_default=None)
    with op.batch_alter_table('review_folders') as batch_op:
        batch_op.alter_column('order', server_default=None)


def downgrade() -> None:
    with op.batch_alter_table('review_folders') as batch_op:
        batch_op.drop_column('order')
    with op.batch_alter_table('knowledge_folders') as batch_op:
        batch_op.drop_column('order')
    with op.batch_alter_table('folders') as batch_op:
        batch_op.drop_column('order')
